import { Component, OnInit } from '@angular/core';
import { HttpService } from "../../core/service/http.service";
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { AuthService } from "../../core/service/auth.service";
import { PagedResultDto, PagerSearchInput } from '../..';
import { NavigationExtras, Router } from '@angular/router';
@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
  noticeTitle;
  notices;
  userType;
  listEntities = new PagedResultDto<any>(0, 0, 1, 4, new Array<any>());
  constructor(
    public http: HttpService,
    private modal: NzModalService,
    public router: Router,
    public Auth: AuthService) {

  }

  ngOnInit() {

    // 判断身份是不是学生
    this.userType = this.Auth.token.userRoleType;
    if (this.userType === 3) {
      this.router.navigate(['app/teacherstudentindexadmin']);
      this.getNotices();
      this.getDrills();
    }
    if (this.userType === 1) {
      this.router.navigate(['app/faculty']);
    }
    if (this.userType === 2) {
      this.router.navigate(['app/teacherstudentindexadmin']);
    }


  }
  async getNotices() {
    const notices = await this.http.get<any>('Notice/GetNoreadedNotices');
    this.notices = new Array();
    this.noticeTitle = "未读通知(" + notices.length + ")";
    if (notices.length !== 0) {
      this.notices = notices;
    }
  }
  async seeNotice(value: any) {
    this.http.get<any>('Notice/ReadNotice?id=' + value.id);
    this.modal.info({
      nzTitle: value.noticeName,
      nzContent: '<p>' + value.noticeContent + '</p>',
      nzOnOk: () => {
        this.getNotices();
      }
    });
  }
  async readAllNotice() {
    await this.http.get<any>('Notice/ReadAllNotice');
    this.getNotices();
  }

  async getDrills() {
    const searchBody = new PagerSearchInput(
      this.listEntities.currentPageIndex,
      this.listEntities.pageSize,
      [],
      []);
    this.listEntities = await this.http.post<PagedResultDto<any>>('DrillRecord/GetPagerDrillRecord', searchBody);
  }

  // 开始演练
  async startDrill(id, drillType) {
    this.modal.confirm({
      nzTitle: '提示',
      nzContent: '是否要开始演练?',
      nzOnOk: async () => {
        if (drillType === 0) {
          const startpar = { Id: id };
          const result = await this.http.post<any>('DrillRecord/StartDrillRecord', startpar);
        }
        const navigationExtras: NavigationExtras = {
          queryParams: { 'id': id }
        };
        this.router.navigate(['/mydrillcontent'], navigationExtras);
      },
      nzOnCancel: () => {

      }
    });
  }
}
